﻿namespace UnityEditor
{
    using System;
    using UnityEngine;

    public class VCWindowRevert : EditorWindow
    {
        private VCAssetList assetList = new VCAssetList();
        private VCListControl revertList = new VCListControl();

        private void DoOpen(VCAssetList revert)
        {
            this.assetList = revert;
            this.RefreshList();
        }

        private static VCWindowRevert GetWindow()
        {
            return EditorWindow.GetWindow<VCWindowRevert>(true, "Version Control Revert");
        }

        public void OnEnable()
        {
            base.position = new Rect(100f, 100f, 700f, 230f);
            base.minSize = new Vector2(700f, 230f);
            this.revertList.ReadOnly = true;
        }

        private void OnGUI()
        {
            GUILayout.Label("Revert Files", EditorStyles.boldLabel, new GUILayoutOption[0]);
            GUILayout.FlexibleSpace();
            Rect screenRect = new Rect(6f, 40f, base.position.width - 12f, base.position.height - 82f);
            GUILayout.BeginArea(screenRect);
            GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true) };
            GUILayout.Box(string.Empty, options);
            GUILayout.EndArea();
            this.revertList.OnGUI(new Rect(screenRect.x + 2f, screenRect.y + 2f, screenRect.width - 4f, screenRect.height - 4f), true);
            GUILayout.FlexibleSpace();
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            if (GUILayout.Button("Cancel", new GUILayoutOption[0]))
            {
                base.Close();
            }
            if ((this.assetList.Count > 0) && GUILayout.Button("Revert", new GUILayoutOption[0]))
            {
                VCProvider.Revert(this.assetList, false).Wait();
                VCDatabase.Invalidate(this.assetList);
                AssetDatabase.Refresh();
                base.Close();
            }
            GUILayout.EndHorizontal();
            GUILayout.Space(12f);
        }

        public static void Open(VCAssetList assets)
        {
            VCTask task = VCProvider.Status(assets);
            task.Wait();
            bool includeFolder = false;
            VCAsset.States[] states = new VCAsset.States[] { VCAsset.States.CheckedOutLocal, VCAsset.States.DeletedLocal, VCAsset.States.AddedLocal, VCAsset.States.Missing };
            VCAssetList revert = task.assetList.Filter(includeFolder, states);
            GetWindow().DoOpen(revert);
        }

        public static void Open(VCChangelist change)
        {
            VCTask task = VCProvider.ChangeStatus(change);
            task.Wait();
            GetWindow().DoOpen(task.assetList);
        }

        private void RefreshList()
        {
            this.revertList.Clear();
            foreach (VCAsset asset in this.assetList)
            {
                this.revertList.Add(null, asset.prettyPath, asset);
            }
            if (this.assetList.Count == 0)
            {
                VCChangelist change = new VCChangelist("no files to revert");
                this.revertList.Add(null, change.description, change).Dummy = true;
            }
            this.revertList.Refresh();
            base.Repaint();
        }
    }
}

